package com.immediatus.graphics.factories;


import com.immediatus.graphics.contracts.IBitmapTextureAtlasSource;
import com.immediatus.graphics.texture.TextureOptions;
import com.immediatus.graphics.texture.bitmap.BitmapTextureAtlas;
import com.immediatus.graphics.texture.region.TextureRegion;
import com.immediatus.graphics.util.BitmapTextureFormat;

public class BitmapTextureAtlasFactory{

    public static BitmapTextureAtlas createForTextureAtlasSourceSize(final BitmapTextureFormat bitmapTextureFormat_, final TextureRegion textureRegion_){
        return BitmapTextureAtlasFactory.createForTextureRegionSize(bitmapTextureFormat_, textureRegion_, TextureOptions.DEFAULT);
    }

    public static BitmapTextureAtlas createForTextureRegionSize(final BitmapTextureFormat bitmapTextureFormat_, final TextureRegion textureRegion_, final TextureOptions textureOptions_){
        final int textureRegionWidth = textureRegion_.getWidth();
        final int textureRegionHeight = textureRegion_.getHeight();
        return new BitmapTextureAtlas(nextPowerOfTwo(textureRegionWidth), nextPowerOfTwo(textureRegionHeight), bitmapTextureFormat_, textureOptions_);
    }

    public static BitmapTextureAtlas createForTextureAtlasSourceSize(final BitmapTextureFormat bitmapTextureFormat_, final IBitmapTextureAtlasSource bitmapTextureAtlasSource_){
        return BitmapTextureAtlasFactory.createForTextureAtlasSourceSize(bitmapTextureFormat_, bitmapTextureAtlasSource_, TextureOptions.DEFAULT);
    }

    public static BitmapTextureAtlas createForTextureAtlasSourceSize(final BitmapTextureFormat bitmapTextureFormat_, final IBitmapTextureAtlasSource bitmapTextureAtlasSource_, final TextureOptions textureOptions_){
        final int textureSourceWidth = bitmapTextureAtlasSource_.getWidth();
        final int textureSourceHeight = bitmapTextureAtlasSource_.getHeight();
        return new BitmapTextureAtlas(nextPowerOfTwo(textureSourceWidth), nextPowerOfTwo(textureSourceHeight), bitmapTextureFormat_, textureOptions_);
    }

    private static int nextPowerOfTwo(final int n) {
        int k = n;
        if (k == 0) return 1;
        k--;
        for (int i = 1; i < 32; i <<= 1) k = k | k >> i;
        return k + 1;
    }

}
